package net.ojbk.timeAxis.action;

import java.io.File;
import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.Part;

import net.ojbk.timeAxis.service.TimeService;
import net.ojbk.timeAxis.tool.StringTool;
import net.ojbk.timeAxis.tool.FileTool;

/**
 * Servlet implementation class timeServlet
 */
@WebServlet("/timeServlet")
@MultipartConfig(
        maxFileSize = 100*1024*1024,
        maxRequestSize = 130*1024*1024
) 
public class timeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public timeServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("---------time---------");
		response.setContentType("text/html;charset=utf-8");
	    response.setCharacterEncoding("utf-8");
	    request.setCharacterEncoding("UTF-8");
		HttpSession session=request.getSession();
	    // 前奏 ↑
		String userId = (String)session.getAttribute("userId");
		if (userId == null || session.getAttribute("login") == null || userId.equals("") || !(boolean)session.getAttribute("login")) { // 未登录 或 错误
			return ; 
		}
		// 判断基本权限 ↑
		String type = request.getParameter("type"); // publishTime 、 
		System.out.println(type);
		TimeService service = new TimeService();
		// 开始前的准备↑
		if (type.equals("publishTime")) { // publishTime
			
		    // 生成 唯一的matchId
		    String timeId = StringTool.getRandomNumberString(); 
	        // 构造 service 层参数
		    String timeTitle = request.getParameter("title");
		    String timeInfo = request.getParameter("content");
		    String axisId = request.getParameter("axisId");
		    System.out.println(axisId);
		    
	        // 配置上传参数 --------------- 开始接收 post 数据 ...
		  
		    // 存储 照片
		    // 赛事文件夹
		    String uploadPosterPath = "C:\\Users\\twobox\\Desktop\\img";
		    // 构造文件夹
		    File tempFile = new File(uploadPosterPath); 
		    if (!tempFile.exists()) {
		    	tempFile.setWritable(true, false);
		    }
		    // 构造完文件夹
		    File uploadPathFile = tempFile; // 
		    if (!uploadPathFile.exists()) { //
		    	uploadPathFile.mkdirs();
		    }
	        
	        Part part1 = request.getPart("img0");
	        String imgId = StringTool.getRandomNumberString(); 
	        String imgFileName = imgId + FileTool.getFileType(part1.getSubmittedFileName());
	        File file1 = new File(uploadPathFile.getPath() + File.separator + imgFileName);
	        // file1 为文件
	        part1.write(file1.getPath()); // 写出
	        
	        String route = imgFileName;
		    // 执行 service
		    service.publishTime(timeId, axisId, timeTitle, timeInfo, new Date(), imgId, route);
		    
		    
		    //  重定向到 赛事介绍页面
			response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
			response.setHeader("Location", "./time.jsp?timeId=" + timeId);
			
		}
		
		
		
	}

}
